import request from '@/utils/request'

// 获取文章列表的api接口函数
export const getArticleByChannel = (channelId, timestamp) => {
  return request({
    url: '/v1_0/articles/',
    params: { channel_id: channelId, timestamp }
  })
}

// 获取文章详情

export const getArticle = (articleId) => {
  return request({
    url: '/v1_0/articles/' + articleId
  })
}

// 获取文章评论
// offset 第一次可以传null,第二次之后是last_id
export const getCommentsByArticle = (articleId, offset) => {
  return request({
    url: '/v1_0/comments',
    params: { type: 'a', source: articleId, offset }
  })
}

// 文章点赞/取消点赞
export const likeArticle = (articleId, isLike) => {
  if (isLike) {
    // 点赞
    return request({
      url: '/v1_0/article/likings',
      method: 'POST',
      data: { target: articleId }
    })
  } else {
    // 取消点赞
    return request({
      url: '/v1_0/article/likings/' + articleId,
      method: 'DELETE'
    })
  }
}

// 收藏/取消收藏文章
export const collectArticle = (articleId, iscollect) => {
  if (iscollect) {
    // 收藏
    return request({
      url: '/v1_0/article/collections',
      method: 'POST',
      data: { target: articleId }
    })
  } else {
    // 取消收藏
    return request({
      url: '/v1_0/article/collections/' + articleId,
      method: 'DELETE'
    })
  }
}

// 获取评论回复
export const getReplyByComment = (commentId, offset) => {
  return request({
    url: '/v1_0/comments',
    params: { type: 'c', source: commentId, offset }
  })
}

// 对文章进行评论 / 对评论进行回复
export const commentOrReply = (target, content, articleId = null) => {
  return request({
    url: '/v1_0/comments',
    method: 'post',
    data: { target, content, art_id: articleId }
  })
}
